# function
unit_boot_service_manager(){
  if [[ ! -f /usr/lib/systemd/system/docker.service ]]
  then
    sudo cp ${DIR}/docker.service /usr/lib/systemd/system/
  fi
}
rpm_install(){
  sudo yum -y install ${DIR}/docker/package/${INSTALL_FILE}
}
tar_install(){
  sudo tar -zxvf ${DIR}/docker/package/${INSTALL_FILE} -C /usr/bin --strip-components=1 --no-same-owner
}
init_docker_user_permission(){
  sudo groupadd docker
  sudo gpasswd -a yapai docker
  sudo chown root:docker /var/run/docker.sock
}
sudo systemctl enable docker
# https://download.docker.com/linux/static/stable/x86_64/
DOCKER_VERSION="24.0.0"
WORK_USER="zxhtom"
DIR=$(cd "$(dirname "$0")";pwd)
while getopts ":d:v:" opt
do
    case $opt in
        d)
        onlie=$OPTARG
        echo -e "\033[40;37m 您输入的路下载选项$ENCRY_NAME\033[0m"
        ;;
        v)
        DOCKER_VERSION=$OPTARG
        echo -e "\033[40;37m 您输入的回滚版本:$TAOS_VERSION\033[0m"
        ;;
        ?)
        echo -e "\033[40;37m 未知参数,你可以这样使用 : sh upgrade.sh -s ~/${WORK_USER}/system-init -btrue -nhello -v3.0.1.3\033[0m"
        exit 1;;
    esac
done
sudo docker --version
if [[ $? -eq 0 ]]
then
  init_docker_user_permission
  exit
fi
if [[ ! -p ${HOME}/${WORK_USER}/docker/package ]]
then
  mkdir -p ${HOME}/${WORK_USER}/docker/package
fi

if [[ "$onlie" == "true" ]]
then
  # TEMPLATE_DOWNLOAD_URL=https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-{{version}}-1.el7.x86_64.rpm
  TEMPLATE_DOWNLOAD_URL=https://download.docker.com/linux/static/stable/x86_64/docker-{{version}}.tgz
  DOWNLOAD_URL=`printf $TEMPLATE_DOWNLOAD_URL | sed 's/{{version}}/'"${DOCKER_VERSION}"'/g'`
  if [[ ! -f ${DOWNLOAD_URL##*/} ]]
  then
    wget -P ${DIR}/docker/package ${DOWNLOAD_URL} -O ${DOWNLOAD_URL##*/}
  fi
fi
INSTALL_FILE=`ls -1t ${DIR}/docker/package`
echo $INSTALL_FILE
if [[ "$INSTALL_FILE" == *".rpm" ]]
then
  rpm_install
elif [[ "$INSTALL_FILE" == *".tgz" ]]
then
  tar_install
else
  echo "not support..."
fi
unit_boot_service_manager
sudo chmod +x /usr/lib/systemd/system/docker.service
sudo systemctl daemon-reload
sudo systemctl enable docker.service
sudo systemctl restart docker
sudo systemctl status docker 2>/dev/null
init_docker_user_permission

